module anengine
import StdEnv, japi

/* This example shows how to deal with timers, in order
   to create an animation. The structure of the example
   was suggested by John Van Groningen. 
****************/

Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame  (WH 400 500) "Engine" js
    (cv, js) = jnewCanvas (WH 400 500) frm  js
    (img_a,js) = load_images js
    cv= jdraw (Txt(90,60) "Otto's Cycle") cv
    cv = drawimage  img_a.[0] cv
    (frm, js) = jshow frm js
    js = loop frm 0 cv js with
            loop :: !F !Int !CP !JS -> *JS
            loop fr sel cv js
              # (x,js) = jsleep 250 js
              # (na,js) = jgetaction x js
              | na==(act fr)
                  # js= jdispose cv js
                  = jquit js
              # cv = drawimage img_a.[sel] cv
              # sel = (sel+1) rem (size img_a)
              = loop fr sel cv js
  = end_japi js world
where
     load_images :: !JS -> (!{#Int},!*JS)
     load_images js
          # (img1,js) = jloadimage "otto1.jpg" js
            (img2,js) = jloadimage "otto2.jpg" js
            (img3,js) = jloadimage "otto3.jpg" js
            (img4,js) = jloadimage "otto4.jpg" js
            (img5,js) = jloadimage "otto5.jpg" js
            (img6,js) = jloadimage "otto6.jpg" js
          =   ({img1,img2,img3,img4,img5,img6},js)

drawimage  img cv = jdraw(Img(90,80) img) cv

 